package com.lebang.controller.buss;

import com.lebang.common.Login;
import com.lebang.service.buss.CustomService;
import com.lebang.service.buss.MemberRecordService;
import com.lebangweb.response.Callback;
import com.lebangweb.response.JsonData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * @author hwq
 */
@Controller
@RequestMapping("/record")
public class RecordController {
    @Autowired
    private MemberRecordService memberRecordService;
    @Autowired
    private CustomService customService;
    @GetMapping("/list")
    @Login
    public String list(Model model,String star, String end) {
        model.addAttribute("customlist",customService.getAllCustomList());
        List<Long> longs=memberRecordService.getSuccess( star,  end);
        Long i=0L;
        for (Iterator<Long> it = longs.iterator(); it.hasNext(); ) {
           i+=it.next();
        }
        model.addAttribute("count1",i/100+"元");
        model.addAttribute("count2",memberRecordService.getTrade( star,  end)/100+"元");
        return "/record/list";
    }
    @PostMapping("/count")
    @ResponseBody@Login
    public JsonData count(String star, String end){
        List<Long> longs=memberRecordService.getSuccess( star,  end);
        Long i=0L;
        for (Iterator<Long> it = longs.iterator(); it.hasNext(); ) {
            if(it.next()!=null){
                i+=it.next();
            }
        }
        Long a=memberRecordService.getTrade( star,  end);
        Map<String,Object> obj=new HashMap<>(1);
        obj.put("count1",i/100+"元");
        obj.put("count2",a/100+"元");
        return Callback.success("200","",obj);
    }
    @GetMapping("/detail")
    public String detail(Model model,String money,String tableName,String taskId) {
        model.addAttribute("tableName",tableName);
        model.addAttribute("taskId",taskId);
        return "/record/detail";
    }
    @PostMapping("/list")
    @ResponseBody@Login
    public Map<String, Object> page(Integer page, Integer limit,String star,String end,String title,String tableName,String userid,String customerid) {
        Map<String, Object> resultMap = new HashMap<>(1);
        try {
            Integer startNum = (page - 1) * limit;
            long count = memberRecordService.findCount(star,end,title,tableName,userid,customerid);
            List<Map<String, Object>> data = memberRecordService.findList( startNum, limit,star,end,title,tableName,userid,customerid);
            resultMap.put("data", data);
            resultMap.put("count", count);
            resultMap.put("code", 0);
        } catch (Exception e) {
            e.printStackTrace();
            resultMap.put("data", null);
            resultMap.put("count", null);
            resultMap.put("code", 200);
        }
        return resultMap;
    }
    @PostMapping("/detail")
    @ResponseBody@Login
    public Map<String, Object> detailPage(Integer page, Integer limit,String tableName,String taskId,String star,String end) {
        Map<String, Object> resultMap = new HashMap<>(1);
        try {
            Integer startNum = (page - 1) * limit;
            long count = memberRecordService.finddetailCount(taskId,star,end);
            List<Map<String, Object>> data = memberRecordService.finddetailList( startNum, limit,tableName,taskId,star,end);
            resultMap.put("data", data);
            resultMap.put("count", count);
            resultMap.put("code", 0);
        } catch (Exception e) {
            e.printStackTrace();
            resultMap.put("data", null);
            resultMap.put("count", null);
            resultMap.put("code", 200);
        }
        return resultMap;
    }
}
